package asteroids;

public class Util {
  public static final double EPSILON = 0.0001;

  public static boolean fuzzyEquals(double x, double y) {
    if (Double.isNaN(x) || Double.isNaN(y))
      return false;
    return Math.abs(x - y) <= EPSILON || Double.valueOf(x).equals(Double.valueOf(y));
  }

  public static boolean fuzzyLessThanOrEqualTo(double x, double y) {
    if (fuzzyEquals(x, y)) {
      return true;
    } else {
      return Double.compare(x, y) < 0;
    }
  }
  
  public static boolean fuzzyMoreThanOrEqualTo(double x, double y){
	  if(fuzzyEquals(x,y)){
		  return true;
	  } else{
		  return Double.compare(x, y) > 0;
	  }
  }
  
  public static boolean fuzzyBetweenOrEqualTo(double x, double min, double max){
	  if(fuzzyMoreThanOrEqualTo(x,min) && fuzzyLessThanOrEqualTo(x,max))
		  return true;
	  else
		  return false;
  }
  
  public static boolean fuzzyLessThan(double x, double y){
	  if(fuzzyLessThanOrEqualTo(x,y) && !fuzzyEquals(x,y))
		  return true;
	  return false;
  }
  
  public static boolean fuzzyMoreThan(double x, double y){
	  if(!fuzzyLessThanOrEqualTo(x,y))
		  return true;
	  return false;
  }
  
  /**
	 * 
	 * @param input
	 * @return 	true if the input value is valid.
	 * 			| result == !(Double.isNaN(input) || Double.isInfinite(input) || input == null)
	 */
	public static boolean isValidDouble(double input){	
		if(Double.isNaN(input) || Double.isInfinite(input)){
			return false;
		}
		return true;
	}
  
  public static double absoluteError(double expected, double actual) {
    return Math.abs(expected - actual);
  }

  public static double relativeError(double expected, double actual) {
    return absoluteError(expected, actual) / Math.abs(expected);
  }
}